package com.sqlcheck.entity;

import com.baomidou.mybatisplus.annotation.*;
import com.sqlcheck.enums.TaskStatus;
import com.sqlcheck.enums.TaskType;
import com.sqlcheck.util.JsonUtils;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;
import lombok.extern.slf4j.Slf4j;

import java.time.LocalDateTime;
import java.util.Map;

@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@EqualsAndHashCode(callSuper = false)
@Accessors(chain = true)
@TableName(value = "scan_tasks")
@Slf4j
public class ScanTask {

    @TableId(value = "id", type = IdType.AUTO)
    private Long id;

    @TableField("task_name")
    private String taskName;

    @TableField("task_type")
    private TaskType taskType;

    @TableField("status")
    private TaskStatus status;

    @TableField("progress")
    private Integer progress;

    @TableField("project_id")
    private Long projectId;

    @TableField("branch_name")
    private String branchName;

    @TableField("user_id")
    private Long userId;

    @TableField("description")
    private String description;

    @TableField("config")
    private String configJson;

    @TableField("source_db_type")
    private String sourceDbType;

    @TableField("target_db_type")
    private String targetDbType;

    @TableField("ai_enhancement_enabled")
    private Boolean aiEnhancementEnabled;

    @TableField("ai_filter_config")
    private String aiFilterConfigJson;

    @TableField("ai_analysis_status")
    private String aiAnalysisStatus;

    @TableField("rule_analysis_completed_at")
    private LocalDateTime ruleAnalysisCompletedAt;

    @TableField("ai_analysis_completed_at")
    private LocalDateTime aiAnalysisCompletedAt;

    @TableField("problem_sql_count")
    private Integer problemSqlCount;

    @TableField("ai_processed_count")
    private Integer aiProcessedCount;

    @TableField("ai_analysis_start_time")
    private LocalDateTime aiAnalysisStartTime;

    @TableField("ai_analysis_end_time")
    private LocalDateTime aiAnalysisEndTime;

    @TableField("ai_analysis_duration")
    private Long aiAnalysisDuration;

    @TableField("ai_processed_sql_count")
    private Integer aiProcessedSqlCount;

    @TableField("ai_skip_reason")
    private String aiSkipReason;

    @TableField("extraction_stats")
    private String extractionStatsJson;

    @TableField("start_time")
    private LocalDateTime startTime;

    @TableField("end_time")
    private LocalDateTime endTime;

    @TableField("error_message")
    private String errorMessage;

    @TableField("report_path")
    private String reportPath;

    @TableField("retry_count")
    private Integer retryCount;

    @TableField("max_retry_count")
    private Integer maxRetryCount;

    @TableField("last_retry_time")
    private LocalDateTime lastRetryTime;

    @TableField(value = "created_at", fill = FieldFill.INSERT)
    private LocalDateTime createdAt;

    @TableField(value = "updated_at", fill = FieldFill.INSERT_UPDATE)
    private LocalDateTime updatedAt;

    @TableLogic
    @TableField("deleted")
    private Boolean deleted;

    // Helper methods for config JSON field
    public Map<String, Object> getConfig() {
        Map<String, Object> result = JsonUtils.safeParseMap(this.configJson);
        return result;
    }

    public ScanTask setConfig(Map<String, Object> config) {
        this.configJson = JsonUtils.safeToJsonString(config);
        return this;
    }

    // Helper methods for extraction stats JSON field
    public Map<String, Object> getExtractionStats() {
        return JsonUtils.safeParseMap(this.extractionStatsJson);
    }

    public ScanTask setExtractionStats(Map<String, Object> extractionStats) {
        this.extractionStatsJson = JsonUtils.safeToJsonString(extractionStats);
        return this;
    }

    // Helper methods for AI filter config JSON field
    public Map<String, Object> getAiFilterConfig() {
        return JsonUtils.safeParseMap(this.aiFilterConfigJson);
    }

    public ScanTask setAiFilterConfig(Map<String, Object> aiFilterConfig) {
        this.aiFilterConfigJson = JsonUtils.safeToJsonString(aiFilterConfig);
        return this;
    }
}